<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>就诊详情录入</title>
    <link rel="stylesheet" type="text/css" href="../css/style.css"/>
    <script type="text/javascript" src="../js/axios.js"></script>
    <script type="text/javascript" src="../js/vue.js"></script>
    <script type="text/javascript" src="../js/message.js"></script>
    <script type="text/javascript" src="../js/urlparse.js"></script>
</head>
<body style="background-color: #ffaa7f;">
<div id="app">
    <fieldset style="margin-top: 100px; border-radius: 10px; border-color: #5555FF;">
        <legend style="text-align: center; color:#5555FF;font-size: 35px;">就诊详情录入</legend>
        <legend style="text-align: center; color:#5555FF;font-size: 25px;">药品</legend>
        <div class="form-group">
            <label>药品编号*:</label>
            <input type="text" v-model="drug.code" @blur="getDrug()"/>
            <span class="msg" v-if="drug.codeFlag">{{drug.codeMsg}}</span>
        </div>
        <div class="form-group">
            <label>药品名:</label>
            <input type="text" readonly="true" v-model="drug.name"/>
        </div>
        <div class="form-group">
            <label>数量*:</label>
            <input type="text" v-model="drug.quantity" @blur="drugQuantityCheck()"/>
            <span class="msg" v-if="drug.quantityFlag">{{drug.quantityMsg}}</span>
        </div>
        <div class="form-group">
            <label></label>
            <button style="font-size: 15px;" @click="addDrug()">添加药品记录</button>
            <span></span>
        </div>
        <legend style="text-align: center; color:#5555FF;font-size: 25px;">项目</legend>
        <div class="form-group">
            <label>项目编号*:</label>
            <input type="text" v-model="project.code" @blur="getProject()"/>
            <span class="msg" v-if="project.codeFlag">{{project.codeMsg}}</span>
        </div>
        <div class="form-group">
            <label>项目名:</label>
            <input type="text" readonly="true" v-model="project.name"/>
        </div>
        <div class="form-group">
            <label>次数*:</label>
            <input type="text" v-model="project.quantity" @blur="projectQuantityCheck()"/>
            <span class="msg" v-if="project.quantityFlag">{{project.quantityMsg}}</span>
        </div>
        <div class="form-group">
            <label></label>
            <button style="font-size: 15px;" @click="addProject()">添加项目记录</button>
            <span></span>
        </div>
        <div class="form-group">
            <label></label>
            <button style="font-size: 15px;" @click="done()">完成</button>
            <span></span>
        </div>
    </fieldset>
</div>
<script>
    var vue = new Vue({
        el: "#app",
        data: {
            healingId: "",
            drug: {
                id: "",
                code: "",
                name: "",
                quantity: 0,
                codeFlag: true,
                codeMsg: "",
                quantityFlag: true,
                quantityMsg: ""
            },
            project: {
                id: "",
                code: "",
                name: "",
                quantity: 0,
                codeFlag: true,
                codeMsg: "",
                quantityFlag: true,
                quantityMsg: ""
            }
        },
        methods: {
            getDrug: function () {
                this.drugCodeCheck();
                if (!this.drug.codeFlag) {
                    axios.post("http://localhost:8000/bracelet/release/api/getdrugdetail",
                        {
                            "drugCode": this.drug.code
                        })
                        .then(res => {
							if(res.data.result == null){
								this.drug.id = "";
								this.drug.name = "";
							}else{
								this.drug.id = res.data.result.drugId;
								this.drug.name = res.data.result.drugName;
							}
                            if(this.drug.name == null || this.drug.name == "") {
								this.drug.codeFlag = true;
                                this.drug.codeMsg = "该药品不存在";
                            }
                        })
                        .catch(error => {
                            console.log(error.message);
                            location.href = "../500.html";
                        });
                }
            },
            addDrug: function () {
                console.log("add");
                if (!this.drug.codeFlag && !this.drug.quantityFlag) {
                    console.log("add2");
                    axios.post("http://localhost:8000/bracelet/release/api/adddrugtohealing",
                        {
                            "healingId": this.healingId,
                            "drugId": this.drug.id,
                            "drugQuantity": this.drug.quantity
                        })
                        .then(res => {
                            if (res.data.result) {
                                alert("添加成功");
                            } else {
                                alert("请勿重复添加");
                            }
                        })
                        .catch(error => {
                            console.log(error.message);
                            location.href = "../500.html";
                        });
                    this.drug.id = "";
                    this.drug.code = "";
                    this.drug.name = "";
                    this.drug.quantity = 0;
                    this.drug.codeFlag = false;
                    this.drug.codeMsg = "";
                    this.drug.quantityFlag = false;
                    this.drug.quantityMsg = "";
                } else {
                    console.log("add3");
                }
            },
            getProject: function () {
                this.projectCodeCheck();
                if (!this.project.codeFlag) {
                    axios.post("http://localhost:8000/bracelet/release/api/getprojectdetail",
                        {
                            "projectCode": this.project.code
                        })
                        .then(res => {
							if(res.data.result == null){
								this.project.id = "";
								this.project.name = "";
							}else{
								this.project.id = res.data.result.projectId;
								this.project.name = res.data.result.projectName;
							}
                            if (this.project.name == null || this.project.name == "") {
								this.project.codeFlag = true;
                                this.project.codeMsg = "该项目不存在";
                            }
                        })
                        .catch(error => {
                            console.log(error.message);
                            location.href = "../500.html";
                        });
                }
            },
            addProject: function () {
                console.log("addstart");
                if (!this.project.codeFlag && !this.project.quantityFlag) {
                    axios.post("http://localhost:8000/bracelet/release/api/addprojecttohealing",
                        {
                            "healingId": this.healingId,
                            "projectId": this.project.id,
                            "projectQuantity": this.project.quantity
                        })
                        .then(res => {
                            if (res.data.result) {
                                alert("添加成功");
                            } else {
                                alert("请勿重复添加");
                            }
                        })
                        .catch(error => {
                            console.log(error.message);
                            location.href = "../500.html";
                        });
                    this.project.id = "";
                    this.project.code = "";
                    this.project.name = "";
                    this.project.quantity = 0;
                    this.project.codeFlag = false;
                    this.project.codeMsg = "";
                    this.project.quantityFlag = false;
                    this.project.quantityMsg = "";
                    console.log("add");
                } else {
                    console.log("failed");
                }
            },
            done: function () {
                location.replace("visitDetails.html?healingId=" + this.healingId);
            },
            drugCodeCheck: function () {
                var reg = /^Y\d{9}$/;
                if (this.drug.code == null || this.drug.code == "") {
                    this.drug.codeMsg = "请输入药品编号"
                    this.drug.codeFlag = true;
                } else if (!reg.test(this.drug.code)) {
                    this.drug.codeMsg = "药品编号共10位，以Y开头";
                    this.drug.name = "";
                    this.drug.codeFlag = true;
                } else {
                    this.drug.codeFlag = false;
                }
            },
            drugQuantityCheck: function () {
                var reg = /^\d+$/;
                if (this.drug.quantity == null || this.drug.quantity <= 0) {
                    this.drug.quantityMsg = "请输入药品数量"
                    this.drug.quantityFlag = true;
                } else if (!reg.test(this.drug.quantity)) {
                    this.drug.quantityMsg = "请输入整数"
                    this.drug.quantityFlag = true;
                } else {
                    this.drug.quantityFlag = false;
                }
            },
            projectCodeCheck: function () {
                var reg = /^X\d{9}$/;
                if (this.project.code == null || this.project.code == "") {
                    this.project.codeMsg = "请输入项目编号"
                    this.project.codeFlag = true;
                } else if (!reg.test(this.project.code)) {
                    this.project.codeMsg = "项目编号共10位，以X开头";
                    this.project.name = "";
                    this.project.codeFlag = true;
                } else {
                    this.project.codeFlag = false;
                }
            },
            projectQuantityCheck: function () {
                var reg = /^\d+$/;
                if (this.project.quantity == null || this.project.quantity <= 0) {
                    this.project.quantityMsg = "请输入项目数量"
                    this.project.quantityFlag = true;
                } else if (!reg.test(this.project.quantity)) {
                    this.project.quantityMsg = "请输入整数"
                    this.project.quantityFlag = true;
                } else {
                    this.project.quantityFlag = false;
                }
            },
        },
        mounted: function () {
            this.healingId = getQueryString("healingId");
        }
    })
</script>
</body>
</html>
